﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;

namespace DataAccess
{
    public static class ActorDataAccess
    {
        public const string ConnString = "Data Source=.\\sqlexpress; Initial Catalog=Brigada; Integrated Security=true";

        public static Actor InsertNewActor(Actor newPerson)
        {
            if (newPerson != null)
            {
                SqlConnection connection = new SqlConnection(ConnString);
                try
                {
                    SqlCommand command = connection.CreateCommand();
                    String text = "INSERT INTO [dbo].[Actor](IdPerson,IdComplaint,Type)";
                    text = text + "VALUES(@IdPerson,@IdComplaint,@Type";
                    command.CommandText = text;

                    command.Parameters.AddWithValue("IdPerson", newPerson.IdPerson);
                    command.Parameters.AddWithValue("IdComplaint", newPerson.IdComplaint);
                    command.Parameters.AddWithValue("Type", newPerson.Type);
                    
                    connection.Open();
                    int result = command.ExecuteNonQuery();
                    if (result <= 0)
                    {
                        return null;
                    }
                    return newPerson;
                }
                catch (Exception ex)
                {
                    return null;
                }
                finally
                {
                    if (connection != null)
                    {
                        connection.Close();
                    }
                    SqlConnection.ClearAllPools();
                }
            }
            return null;
        }
    }
}
